package com.scheduling.material.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scheduling.common.result.Result;
import com.scheduling.material.dto.MaterialDTO;
import com.scheduling.material.dto.MaterialQueryDTO;
import com.scheduling.material.entity.Material;
import com.scheduling.material.service.MaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 原料管理控制器
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Slf4j
@RestController
@RequestMapping("/api/material")
@Tag(name = "原料管理", description = "原料信息的增删改查操作")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @Operation(summary = "分页查询原料列表", description = "根据条件分页查询原料信息")
    @PostMapping("/page")
    public Result<IPage<Material>> getMaterialPage(@RequestBody MaterialQueryDTO queryDTO) {
        try {
            IPage<Material> page = materialService.getMaterialPage(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            log.error("分页查询原料列表失败：{}", e.getMessage(), e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Operation(summary = "根据ID获取原料详情", description = "根据原料ID获取详细信息")
    @GetMapping("/{materialId}")
    public Result<Material> getMaterialById(
            @Parameter(description = "原料ID", required = true) @PathVariable String materialId) {
        try {
            Material material = materialService.getMaterialById(materialId);
            if (material == null) {
                return Result.error("原料不存在");
            }
            return Result.success(material);
        } catch (Exception e) {
            log.error("获取原料详情失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "添加原料", description = "添加新的原料信息")
    @PostMapping
    public Result<String> addMaterial(@Validated @RequestBody MaterialDTO materialDTO) {
        try {
            boolean success = materialService.addMaterial(materialDTO);
            if (success) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (Exception e) {
            log.error("添加原料失败：{}", e.getMessage(), e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    @Operation(summary = "更新原料", description = "更新原料信息")
    @PutMapping
    public Result<String> updateMaterial(@Validated @RequestBody MaterialDTO materialDTO) {
        try {
            boolean success = materialService.updateMaterial(materialDTO);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新原料失败：{}", e.getMessage(), e);
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    @Operation(summary = "删除原料", description = "根据ID删除原料")
    @DeleteMapping("/{materialId}")
    public Result<String> deleteMaterial(
            @Parameter(description = "原料ID", required = true) @PathVariable String materialId) {
        try {
            boolean success = materialService.deleteMaterial(materialId);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除原料失败：{}", e.getMessage(), e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "批量删除原料", description = "根据ID列表批量删除原料")
    @DeleteMapping("/batch")
    public Result<String> deleteMaterials(@RequestBody List<String> materialIds) {
        try {
            boolean success = materialService.deleteMaterials(materialIds);
            if (success) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除原料失败：{}", e.getMessage(), e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "获取所有原料列表", description = "获取所有原料的简要信息，用于下拉选择")
    @GetMapping("/all")
    public Result<List<Material>> getAllMaterials() {
        try {
            List<Material> materials = materialService.getAllMaterials();
            return Result.success(materials);
        } catch (Exception e) {
            log.error("获取所有原料列表失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "检查原料名称是否存在", description = "检查原料名称是否已被使用")
    @GetMapping("/check-name")
    public Result<Boolean> checkMaterialNameExists(
            @Parameter(description = "原料名称", required = true) @RequestParam String materialName,
            @Parameter(description = "排除的原料ID（更新时使用）") @RequestParam(required = false) String excludeId) {
        try {
            boolean exists = materialService.checkMaterialNameExists(materialName, excludeId);
            return Result.success(exists);
        } catch (Exception e) {
            log.error("检查原料名称失败：{}", e.getMessage(), e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }
} 